package com.hengxinguotong.hxgtwygsp.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by pc on 2018-01-31.
 */

public class MyOpenHelper extends SQLiteOpenHelper
{
    private static final String DATABASE_NAME = "HXGTWYGSP.db";
    private static final int NEW_VERSION = 1;

    public static final String DOWNLOAD_INFO = "download_info";//断点下载表
    public static final String T_VIDEO = "t_video";//视频表
//    public static final String T_PICTURE = "t_picture";//图片表
//    public static final String T_TEXT = "t_text";//视频表

    private static volatile SQLiteDatabase mSQLiteDatabase = null;

    public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
    {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase)
    {
        String createDownload = "CREATE TABLE IF NOT EXISTS " + DOWNLOAD_INFO +
                "(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "thread_id INTEGER," +
                "start_pos INTEGER," +
                "end_pos INTEGER," +
                "compelete_size INTEGER," +
                "url VARCHAR(1000));";
        String createVideo = "CREATE TABLE IF NOT EXISTS " + T_VIDEO +
                "(_id INTEGER," +
                "_url VARCHAR(1024)," +//视频地址
                "_image VARCHAR(1024)," +//预览图片
                "_status INTEGER," +
                "_deadline VARCHAR(64));";
        sqLiteDatabase.execSQL(createDownload);
        sqLiteDatabase.execSQL(createVideo);

        onUpgrade(sqLiteDatabase, 1, NEW_VERSION);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        for (int i = oldVersion; i < newVersion; i++)
        {
            switch (i)
            {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                default:
                    break;
            }
        }
    }

    public static SQLiteDatabase getInstance(Context context)
    {
        if (mSQLiteDatabase == null)
        {
            synchronized (MyOpenHelper.class)
            {
                if (mSQLiteDatabase == null)
                {
                    MyOpenHelper myOpenHelper = new MyOpenHelper(context, DATABASE_NAME, null, NEW_VERSION);
                    mSQLiteDatabase = myOpenHelper.getWritableDatabase();
                }
            }
        }
        return mSQLiteDatabase;
    }

    public static void closeDB()
    {
        if (mSQLiteDatabase != null)
        {
            mSQLiteDatabase.close();
        }
    }
}
